package app.qrcode.browser.caculationtest

import android.app.Activity
import android.content.Context
import android.content.DialogInterface
import android.os.Bundle
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.NavController

import androidx.navigation.Navigation
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.NavigationUI


class MainActivity : AppCompatActivity() {
    lateinit var controller: NavController
    lateinit var navHostFragment: NavHostFragment

    /*val controller = Activity.findNavController(R.id.fragmentContainerView)*/
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        navHostFragment = supportFragmentManager.findFragmentById(R.id.fragmentContainerView) as NavHostFragment
        controller=navHostFragment.navController
        NavigationUI.setupActionBarWithNavController(this, controller)
    }

    override fun onSupportNavigateUp(): Boolean {
        if (controller.currentDestination?.id == R.id.questionFragment) {
            val builder = AlertDialog.Builder(this)
            builder.setTitle(getString(R.string.quit_dialog_title))
            builder.setPositiveButton(R.string.dialog_positive_message,DialogInterface.OnClickListener{_,_->
                controller.navigateUp()
            })
            builder.setNegativeButton(R.string.dialog_nagative_cancel,DialogInterface.OnClickListener{_,_->

            })
            val dialog: AlertDialog = builder.create()
            dialog.show()
        }
        return super.onNavigateUp()
    }

    override fun onBackPressed() {
        onSupportNavigateUp()
    }
    }
